package com.woniuedu.server.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.woniuedu.server.VodService;
import com.woniuedu.utils.InitObject;
import com.woniuedu.utils.VodUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author：向天浩
 * @Date：2022/4/18 10:18
 * @Description :TODO
 */
@Service
public class VodServiceImpl implements VodService {
    /**
     *功能描述 添加视频
     * @Author: 向天浩
     * @Date: 2022/4/19 12:08
     * @param file
     * @return java.lang.String
     */
    @Override
    public String uploadAlyVideo(MultipartFile file) {

        try {
            String KeyId= VodUtils.KEYID;
            System.out.println(KeyId);
            String KeySecret=VodUtils.KEYSECRET;
            System.out.println(KeySecret);
            //上传文件原始名称：比如3496.mp4文件
            String fileName= file.getOriginalFilename();
            System.out.println(fileName);
            //上传之后显示的名称这里将上面的原始文件名称进行了操作分割了后缀。变成3496文件
            String title=fileName.substring(0,fileName.lastIndexOf("."));
            //上传文件输入流。这里会有异常，所以使用try-catch包裹了
            InputStream inputStream = file.getInputStream();
            //上传对象
            UploadStreamRequest request = new UploadStreamRequest(KeyId,KeySecret,title,fileName,inputStream);
            /* 是否使用默认水印(可选)，指定模板组ID时，根据模板组配置确定是否使用默认水印*/
            //request.setShowWaterMark(true);
            /* 自定义消息回调设置，参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
            //request.setUserData(""{\"Extend\":{\"test\":\"www\",\"localId\":\"xxxx\"},\"MessageCallback\":{\"CallbackURL\":\"http://test.test.com\"}}"");
            /* 视频分类ID(可选) */
            request.setCateId(1000397227L);
            /* 视频标签,多个用逗号分隔(可选) */
            //request.setTags("标签1,标签2");
            /* 视频描述(可选) */
            //request.setDescription("视频描述");
            /* 封面图片(可选) */
            //request.setCoverURL("http://cover.sample.com/sample.jpg");
            /* 模板组ID(可选) */
            request.setTemplateGroupId("0b3e531f93b042b03c8760eecec1b58f");
            /* 工作流ID(可选) */
            //request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
            /* 存储区域(可选) */
            //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
            /* 开启默认上传进度回调 */
            // request.setPrintProgress(true);
            /* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
            // request.setProgressListener(new PutObjectProgressListener());
            /* 设置应用ID*/
            //request.setAppId("app-1000000");
            /* 点播服务接入点 */
            request.setApiRegionId("cn-shanghai");
            /* ECS部署区域*/
            // request.setEcsRegionId("cn-shanghai");
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return videoId;  //将视频id返回，保存到数据库中而不是保存视频地址
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     *功能描述 删除视频
     * @Author: 向天浩
     * @Date: 2022/4/19 12:08
     * @param videoId
     * @return void
     */
    @Override
    public void removeVideo(String videoId) {
        try {
            //获取初始化对象
            DefaultAcsClient client = InitObject.initVodClient();
            DeleteVideoRequest request=new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *功能描述 批量删除视频
     * @Author: 向天浩
     * @Date: 2022/4/19 14:26
     * @param videoList
     * @return void
     */
    @Override
    public void removelist(List<String> videoList) {
        try {
            DefaultAcsClient client = InitObject.initVodClient();
            DeleteVideoRequest request = new DeleteVideoRequest();
            String join = StringUtils.join(videoList.toArray(), ",");
            request.setVideoIds(join);
            client.getAcsResponse(request);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *功能描述 获取播放凭证
     * @Author: 向天浩
     * @Date: 2022/4/21 12:10
     * @param videoId
     * @return java.lang.String
     */
    @Override
    public String getAuth(String videoId) {
        try {
            //获取vod的初始化对象
            DefaultAcsClient client = InitObject.initVodClient();
            GetVideoPlayAuthRequest request=new GetVideoPlayAuthRequest();
            //将id放入
            request.setVideoId(videoId);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //返回播放凭证
            return response.getPlayAuth();
        }catch (Exception e) {
            return null;
        }
    }


}
